package com.lebang.service.buss;

import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.TbMemberInviteStatistics;
import com.lebang.mapper.TbMemberInviteStatisticsMapper;
import com.lebang.mapper.buss.MemberInviteMapper;
import com.lebang.model.Whether;
import com.lebang.model.enums.Booleans;
import com.lebangweb.common.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: tony
 * @Date: 2019-01-24 14:35
 * @Description:
 */
@Service
public class MemberInviteStatisticsService {
    private static final Logger log = LoggerFactory.getLogger(MemberInviteStatisticsService.class);
    @Autowired
    private MemberInviteMapper memberInviteMapper;
    @Autowired
    private TbMemberInviteStatisticsMapper tbMemberInviteStatisticsMapper;

    public TbMemberInviteStatistics getByMemberId(String memberId) {

        return tbMemberInviteStatisticsMapper.getByMemberId(memberId);
    }

    public List<Map<String, Object>> listTop(int num) {
        return memberInviteMapper.listTop(num);
    }

    /**
     * 当会员新推荐一个有效的徒弟时调用的方法
     *
     * @param memberId
     * @return
     */
    public void addNewMember(String memberId, RequestHeaders header) {
        try {
            TbMemberInviteStatistics memberInviteStatistics = getByMemberId(memberId);
            if (memberInviteStatistics == null) {
                memberInviteStatistics = new TbMemberInviteStatistics();
                memberInviteStatistics.setCreateTime(new Date());
                memberInviteStatistics.setEnable(Whether.YES.getDbIndex());
                memberInviteStatistics.setExpectMoney(0);
                memberInviteStatistics.setMemberId(memberId);
                memberInviteStatistics.setModifyTime(new Date());
                memberInviteStatistics.setMonthValid(1);
                memberInviteStatistics.setNumbers(1);
                memberInviteStatistics.setLastMonthValid(0);
                memberInviteStatistics.setRebateProportion(new BigDecimal("0.1"));
                memberInviteStatistics.setTotalMoney(0);
                memberInviteStatistics.setHasDeleted(Booleans.False.name());
                memberInviteStatistics.setTotalTsMoney(0);
                tbMemberInviteStatisticsMapper.insertSelective(memberInviteStatistics);
            } else {

//                checkMemberGrade(memberId);

                int number = memberInviteStatistics.getNumbers() + 1;
                int monthValid = memberInviteStatistics.getMonthValid() + 1;
                memberInviteStatistics.setEnable(Whether.YES.getDbIndex());
                memberInviteStatistics.setExpectMoney(0);
                memberInviteStatistics.setMemberId(memberId);
                memberInviteStatistics.setMonthValid(monthValid);
                memberInviteStatistics.setNumbers(number);


                tbMemberInviteStatisticsMapper.updateByPrimaryKeySelective(memberInviteStatistics);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    public int update(TbMemberInviteStatistics memberInviteStatistics) {
        return tbMemberInviteStatisticsMapper.updateByPrimaryKeySelective(memberInviteStatistics);
    }

    public void addTotalAmount(String memberId, Integer amount,int type) {
        if (type==0){
            //徒弟收益累计
            tbMemberInviteStatisticsMapper.addTotalAmount(memberId,amount);
        }else if(type==1){
            //徒孙收益累计
            tbMemberInviteStatisticsMapper.addTotalTsAmount(memberId,amount);
        }

    }

    //徒孙数量
    public int getTsNumbers(String memberId) {
        return tbMemberInviteStatisticsMapper.selectTsNumbersByMemberId(memberId);
    }
}
